<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="createTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="payTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" stripe border max-height="600" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" width="200" prop="orderNo" >
        <template slot-scope="scope">
          <router-link :to="{ path: '/order/order-info', query: {orderNo: scope.row.orderNo} } " class="link-type">
            <span>{{ scope.row.orderNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <el-tag>{{orderStatusFormat(scope.row.orderStatus)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收货人" align="center" prop="consignee" width="160" />
      <el-table-column label="联系电话" align="center" prop="phone" width="150" />
      <el-table-column label="收货地址" align="center" prop="address" width="300" />
      <el-table-column label="商品件数" align="center" prop="totalNum" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="打折金额" align="center" prop="discountAmount" />
      <el-table-column label="优惠金额" align="center" prop="couponAmount" />
      <el-table-column label="运费" align="center" prop="freight" />
      <el-table-column label="实付金额" align="center" prop="payAmount" />
      <el-table-column label="买家留言" align="center" prop="message" width="260" :show-overflow-tooltip="true" />
      <el-table-column label="下单时间" align="center" prop="createTime" width="180" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
      <el-table-column label="收货时间" align="center" prop="confirmTime" width="180" />
      <el-table-column label="取消时间" align="center" prop="cancelTime" width="180" />
      <el-table-column label="备注" align="center" prop="remark" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="修改人" align="center" prop="updateBy" width="60"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleInfo(scope.row)"
            v-hasPermi="['system:item:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-truck"
            @click="handleSend(scope.row)"
            v-if="scope.row.orderStatus == 2"
            v-hasPermi="['system:item:edit']"
          >发货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog :visible.sync="sendOpen" title="填写发货信息">
      <el-form
        ref="sendForm"
        :model="sendForm"
        :rules="sendRules"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="sendForm.orderNo" disabled/>
        </el-form-item>
        <el-form-item label="物流公司" prop="logisticsCompany">
          <el-input v-model="sendForm.logisticsCompany" />
        </el-form-item>
        <el-form-item label="物流单号" prop="logisticsNo">
          <el-input v-model="sendForm.logisticsNo" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="sendOpen = false">取消</el-button>
        <el-button type="primary" @click="handleSendSubmit">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listOrder, sendOrder } from "@/api/order/order";

  export default {
    name: "OrderList",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 状态
        orderStatusOptions: [],
        createTime: [],
        payTime: [],
        // 查询参数
        queryParams: {
          page: 1,
          size: 20,
          orderNo: undefined,
          orderStatus: 2,
          consignee: undefined
        },
        sendOpen: false,
        sendForm: {},
        sendRules: {
          orderNo: [{ required: true, trigger: "blur", message: "订单号不能为空" }],
          logisticsCompany: [{ required: true, trigger: "blur", message: "物流公司不能为空" }],
          logisticsNo: [{ required: true, trigger: "blur", message: "物流单号不能为空" }]
        },
      };
    },
    created() {
      this.getList();
      this.getOptions("order-status").then(response => {
        this.orderStatusOptions = response.data.options;
      });
    },
    methods: {
      /** 查询商品类型列表 */
      getList() {
        this.loading = true;
        const payload = {
          ...this.queryParams,
          createTime: this.parseDataRange(this.createTime),
          payTime: this.parseDataRange(this.payTime)
        };
        listOrder(payload).then(response => {
            this.list = response.data.rows;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 状态翻译
      orderStatusFormat(data) {
        return this.selectOptionValue(this.orderStatusOptions, data);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.createTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查看订单详情 */
      handleInfo(row) {
        this.$router.push({path: '/service/service-info', query: {serviceNo: row.serviceNo}});
      },
      /** 订单发货 */
      handleSend(row) {
        this.resetForm("sendForm");
        this.sendForm = {
          orderNo: row.orderNo
        }
        this.sendOpen = true;
      },
      handleSendSubmit() {
        this.$refs.sendForm.validate(valid => {
          if (valid) {
            sendOrder(this.sendForm.orderNo, {
              logisticsCompany: this.sendForm.logisticsCompany,
              logisticsNo: this.sendForm.logisticsNo
            }).then(res => {
              this.sendOpen = false;
              this.handleQuery();
            })
          } else {
            return false;
          }
        });
      }
    }
  };
</script>
